package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.AdministrationMapper;
import com.cqjtu.drive.mapper.UserMapper;
import com.cqjtu.drive.pojo.Administration;
import com.cqjtu.drive.pojo.User;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.AdministrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdministrationServiceImpl implements AdministrationService {

    @Autowired
    private AdministrationMapper administrationMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public Response<Administration> getAdministrationById(String administrationId) {
        Administration administration = administrationMapper.getAdministrationById(administrationId);
        if (administration != null) {
            return new Response<>(true, "Success", administration);
        } else {
            return new Response<>(false, "Administration not found", null);
        }
    }

    @Override
    public Response<String> insertAdministration(Administration administration) {
        try {
            administrationMapper.insertAdministration(administration);
            return new Response<>(true, "Insert success", null);
        } catch (Exception e) {
            return new Response<>(false, "Insert failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<String> updateAdministration(Administration administration) {
        try {
            administrationMapper.updateAdministration(administration);
            return new Response<>(true, "Update success", null);
        } catch (Exception e) {
            return new Response<>(false, "Update failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<String> deleteAdministration(String administrationId) {
        try {
            Administration administration = administrationMapper.getAdministrationById(administrationId);
            administrationMapper.deleteAdministration(administrationId);
            userMapper.deleteUser(administration.getUserId());
            return new Response<>(true, "Delete success", null);
        } catch (Exception e) {
            return new Response<>(false, "Delete failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<List<Administration>> getAllAdministrations() {
        List<Administration> administrations = administrationMapper.getAllAdministrations();
        return new Response<>(true, "Success", administrations);
    }
}
